package com.atf.atfconfigurator.views;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class JVMPropertiesView extends VerticalLayout implements View {
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        Label section = new Label("Process Definition");
        section.addStyleName("h3");
        section.addStyleName("colored");
        form.addComponent(section);
        //initialHeapSize
        final TextField initialHeapSize = new TextField("Initial Heap Size");
        initialHeapSize.setWidth("50%");
        initialHeapSize.setValue(wasConfigProperty.getString("initialHeapSize",""));
        initialHeapSize.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("initialHeapSize", value);
			}
        	
        });
        form.addComponent(initialHeapSize);
       //maximumHeapSize
        final TextField maximumHeapSize = new TextField("Maximum Heap Size");
        maximumHeapSize.setWidth("50%");
        maximumHeapSize.setValue(wasConfigProperty.getString("maximumHeapSize",""));
        maximumHeapSize.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("maximumHeapSize", value);
			}
        	
        });
        form.addComponent(maximumHeapSize);
        
        //changeWorkingDirectory
        CheckBox changeWorkingDirectory = new CheckBox("Change Working Directory");
        if( wasConfigProperty.getString("changeWorkingDirectory", "NO").equalsIgnoreCase("YES")){
        	changeWorkingDirectory.setValue(true);
        }
        changeWorkingDirectory.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeWorkingDirectory", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeWorkingDirectory", "NO");
				}
			}        	
        });
        form.addComponent(changeWorkingDirectory);
       //enableVerboseClassLoading
        CheckBox enableVerboseClassLoading = new CheckBox("Enable Verbose ClassLoading");
        if( wasConfigProperty.getString("enableVerboseClassLoading", "NO").equalsIgnoreCase("YES")){
        	enableVerboseClassLoading.setValue(true);
        }
        enableVerboseClassLoading.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("enableVerboseClassLoading", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("enableVerboseClassLoading", "NO");
				}
			}        	
        });
        form.addComponent(enableVerboseClassLoading);
        
       //enableVerboseGarbageCollection
        CheckBox enableVerboseGarbageCollection = new CheckBox("Enable Verbose Garbage Collection");
        if( wasConfigProperty.getString("enableVerboseGarbageCollection", "NO").equalsIgnoreCase("YES")){
        	enableVerboseGarbageCollection.setValue(true);
        }
        enableVerboseGarbageCollection.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("enableVerboseGarbageCollection", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("enableVerboseGarbageCollection", "NO");
				}
			}        	
        });
        form.addComponent(enableVerboseGarbageCollection);
        
        
        
      //jvmClassPath
        final TextArea jvmClassPath = new TextArea("Class Path");
        jvmClassPath.setWidth("50%");
        jvmClassPath.setValue(wasConfigProperty.getString("jvmClassPath",""));
        jvmClassPath.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("jvmClassPath", value);
			}
        	
        });
        form.addComponent(jvmClassPath);
        
       //jvmClassPath
        final TextArea genericJvmArguments = new TextArea("Generic Jvm Arguments");
        genericJvmArguments.setWidth("50%");
        genericJvmArguments.setValue(wasConfigProperty.getString("genericJvmArguments",""));
        genericJvmArguments.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("genericJvmArguments", value);
			}
        	
        });
        form.addComponent(genericJvmArguments);
        
        
        //Node Monitoring Policy
        NativeSelect nodeRestartState = new NativeSelect("Node Monitoring Policy");
        nodeRestartState.setWidth("50%");
        nodeRestartState.setNullSelectionAllowed(false);
        
        List<Object> nodeRestartStateName=WasConstant.getList(WasConstant.WAS_XML_JVM_NAME+".nodeRestartPolicies");
	     for(Object th:nodeRestartStateName){
	    	 nodeRestartState.addItem((String)th);
	     }
        nodeRestartState.setValue(wasConfigProperty.getString("nodeRestartState","PREVIOUS"));
        nodeRestartState.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("nodeRestartState", value);
			}
        	
        });
        form.addComponent(nodeRestartState);
        
        
        
        //Core Group Service
        CheckBox coreGroupService = new CheckBox("Core Group Service");
        if( wasConfigProperty.getString("coreGroupService", "true").equalsIgnoreCase("true")){
        	coreGroupService.setValue(true);
        }
        coreGroupService.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("coreGroupService", "true");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("coreGroupService", "false");
				}
			}        	
        });
        form.addComponent(coreGroupService);
        
        
        
        section = new Label("Process Logs");
        section.addStyleName("h3");
        section.addStyleName("colored");
        form.addComponent(section);
        
      //changeProcessLogs
        CheckBox changeProcessLogs = new CheckBox("Change Process Logs");
        if( wasConfigProperty.getString("changeProcessLogs", "YES").equalsIgnoreCase("YES")){
        	changeProcessLogs.setValue(true);
        }
        changeProcessLogs.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeProcessLogs", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeProcessLogs", "NO");
				}
			}        	
        });
        form.addComponent(changeProcessLogs);
        
       //changeTraceLogs
        CheckBox changeTraceLogs = new CheckBox("Change Trace Logs");
        if( wasConfigProperty.getString("changeTraceLogs", "YES").equalsIgnoreCase("YES")){
        	changeTraceLogs.setValue(true);
        }
        changeTraceLogs.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeTraceLogs", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeTraceLogs", "NO");
				}
			}        	
        });
        form.addComponent(changeTraceLogs);
        
      //changeServiceLogs
        CheckBox changeServiceLogs = new CheckBox("Change Service Logs");
        if( wasConfigProperty.getString("changeServiceLogs", "YES").equalsIgnoreCase("YES")){
        	changeServiceLogs.setValue(true);
        }
        changeServiceLogs.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeServiceLogs", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("changeServiceLogs", "NO");
				}
			}        	
        });
        form.addComponent(changeServiceLogs);
        
        
        //rolloverType
        final TextField rolloverType = new TextField("Logs Roll Over Type");
        rolloverType.setWidth("50%");
        rolloverType.setValue(wasConfigProperty.getString("rolloverType","BOTH"));
        rolloverType.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("rolloverType", value);
			}
        	
        });
        form.addComponent(rolloverType);
        
      //rolloverSize
        final TextField rolloverSize = new TextField("Logs Roll Over Size");
        rolloverSize.setWidth("50%");
        rolloverSize.setValue(wasConfigProperty.getString("rolloverSize",""));
        rolloverSize.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("rolloverSize", value);
			}
        	
        });
        form.addComponent(rolloverSize);
        
      //rolloverSize
        final TextField maxNumberOfBackupFiles = new TextField("Max Logs Backup ");
        maxNumberOfBackupFiles.setWidth("50%");
        maxNumberOfBackupFiles.setValue(wasConfigProperty.getString("maxNumberOfBackupFiles",""));
        maxNumberOfBackupFiles.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("maxNumberOfBackupFiles", value);
			}
        	
        });
        form.addComponent(maxNumberOfBackupFiles);
        
        
        section = new Label("Process Execution");
        section.addStyleName("h3");
        section.addStyleName("colored");
        form.addComponent(section);
        
       //umask
        final TextField umask = new TextField("User Mask ");
        umask.setWidth("50%");
        umask.setValue(wasConfigProperty.getString("umask",""));
        umask.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("umask", value);
			}
        	
        });
        form.addComponent(umask);
        
        //runAsUser
        final TextField runAsUser = new TextField("Run As User ");
        runAsUser.setWidth("50%");
        runAsUser.setValue(wasConfigProperty.getString("runAsUser",""));
        runAsUser.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("runAsUser", value);
			}
        	
        });
        form.addComponent(runAsUser);
        
       //runAsUser
        final TextField runAsGroup = new TextField("Run As Group ");
        runAsGroup.setWidth("50%");
        runAsGroup.setValue(wasConfigProperty.getString("runAsGroup",""));
        runAsGroup.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("runAsGroup", value);
			}
        	
        });
        form.addComponent(runAsGroup);
        
        
        //Application Server classloader 
        section = new Label("Application Server Classloader ");
        section.addStyleName("h3");
        section.addStyleName("colored");
        form.addComponent(section);
        
        final TextField createApplicationServerClassloader = new TextField("Number of Application Server Classloader ");
	     int createApplicationServerClassloaderValue=wasConfigProperty.getInt("createApplicationServerClassloader",0);
	     createApplicationServerClassloader.setValue(String.valueOf(createApplicationServerClassloaderValue));
	     createApplicationServerClassloader.setWidth("50%");
	     form.addComponent(createApplicationServerClassloader);
	     
	     
	     createApplicationServerClassloader.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("createApplicationServerClassloader",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("JVMProperties");
				}        	
	        });
	     
	     if(createApplicationServerClassloaderValue > 0){
	    	 for(int i=1; i<createApplicationServerClassloaderValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getApplicationServerClassloader(i), false));
	    	 }
	     }
        
        	
        
	}
	private Component getApplicationServerClassloader(final int pos) {
		// TODO Auto-generated method stub
	
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        
        
      //Application ClassLoader Policy
        NativeSelect applicationClassLoaderPolicy = new NativeSelect("Application ClassLoader Policy");
        applicationClassLoaderPolicy.setWidth("50%");
        applicationClassLoaderPolicy.setNullSelectionAllowed(false);
        
        List<Object> applicationClassLoaderPolicyName=WasConstant.getList(WasConstant.WAS_XML_JVM_NAME+".applicationClassLoaderPolicy");
	     for(Object th:applicationClassLoaderPolicyName){
	    	 applicationClassLoaderPolicy.addItem((String)th);
	     }
	     applicationClassLoaderPolicy.setValue(wasConfigProperty.getString("applicationClassLoaderPolicy."+pos,""));
	     applicationClassLoaderPolicy.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("applicationClassLoaderPolicy."+pos, value);
			}
        	
        });
        form.addComponent(applicationClassLoaderPolicy);
        
       //Application ClassLoading Mode
        NativeSelect applicationClassLoadingMode = new NativeSelect("Application ClassLoading Mode");
        applicationClassLoadingMode.setWidth("50%");
        applicationClassLoadingMode.setNullSelectionAllowed(false);
        
        List<Object> applicationClassLoadingModeName=WasConstant.getList(WasConstant.WAS_XML_JVM_NAME+".applicationClassLoadingMode");
	     for(Object th:applicationClassLoadingModeName){
	    	 applicationClassLoadingMode.addItem((String)th);
	     }
	     applicationClassLoadingMode.setValue(wasConfigProperty.getString("applicationClassLoadingMode."+pos,""));
	     applicationClassLoadingMode.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("applicationClassLoadingMode."+pos, value);
			}
        	
        });
        form.addComponent(applicationClassLoadingMode);
        
        
        //Application ClassLoading Mode
        NativeSelect libraryName = new NativeSelect("Shared Library Name");
        //libraryName.setWidth("50%");
        libraryName.setNullSelectionAllowed(false);
        
        if( wasConfigProperty.getInt("webSphereSharedLibrary",0) > 0){
        	Iterator<String> iterator=wasConfigProperty.getKeys("webSphereSharedLibraryName");
        	while(iterator.hasNext()) {
                String element = iterator.next();              
                libraryName.addItem(wasConfigProperty.getString(element));
             }
        }
        String value=wasConfigProperty.getString("libraryName."+pos,"");
		libraryName.setValue(value);
        
	     libraryName.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("libraryName."+pos, value);
			}
        	
        });
        form.addComponent(libraryName);
        
        
    	return form;
	}
}
